package com.resou.reader.mine.vipcenter.monthlybooks.fragment;


import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.resou.reader.R;
import com.resou.reader.api.entry.BookCategoryBean;
import com.resou.reader.api.entry.FilterBean;
import com.resou.reader.base.ResouBaseFragment;
import com.resou.reader.bookdetail.BookDetailActivity;
import com.resou.reader.data.bookstore.model.PlateNovels;
import com.resou.reader.mine.vipcenter.adapter.MonthlyMoreAdapter;
import com.resou.reader.mine.vipcenter.callback.IFilterViewCallbackListener;
import com.resou.reader.mine.vipcenter.monthlybooks.IMonthlyBooksContract;
import com.resou.reader.mine.vipcenter.monthlybooks.MonthlyBooksPresenter;
import com.resou.reader.utils.ToastUtil;
import com.resou.reader.view.FilterView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.List;

import butterknife.BindView;

/**
 * A simple {@link Fragment} subclass.
 */
public class Fragment_monthly_gril extends ResouBaseFragment<MonthlyBooksPresenter> implements IMonthlyBooksContract.IMonthlyBooksView {

    @BindView(R.id.tvHeader)
    TextView tvHeader;
    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.mSmartRefreshLayout)
    SmartRefreshLayout mSmartRefreshLayout;
    int cate = -1, word = -1, serial = -1, channel = 2, page = 0;
    String mCateLabel = "";
    String mWordLabel = "";
    String mSerialLabel = "";
    MonthlyMoreAdapter monthlyMoreAdapter;
    FilterView filterView;
    boolean isCliable = true;

    public Fragment_monthly_gril() {
    }


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_fragment_monthly_boy;
    }

    @Override
    protected void initPresenter() {
        if (mPresenter == null) {
            mPresenter = new MonthlyBooksPresenter(this);
        }
    }

    @Override
    protected void initView() {
        filterView = new FilterView(getActivity());
        monthlyMoreAdapter = new MonthlyMoreAdapter(R.layout.adapter_monthlymore, null);
        monthlyMoreAdapter.setShowFree(false);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRecyclerView.setAdapter(monthlyMoreAdapter);
        monthlyMoreAdapter.addHeaderView(filterView.getFilterView());
        monthlyMoreAdapter.openLoadAnimation(BaseQuickAdapter.SCALEIN);
        mSmartRefreshLayout.setDisableContentWhenRefresh(true);
        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (getScollYDistance(recyclerView) >= filterView.getmHeight()) {
                    isHeader(true);
                } else {
                    isHeader(false);
                }
            }
        });
        tvHeader.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isCliable) {
                    isHeader(false);
                    mRecyclerView.scrollToPosition(0);
                    mRecyclerView.smoothScrollToPosition(0);
                }
            }
        });
        monthlyMoreAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                PlateNovels plateNovels = (PlateNovels) adapter.getData().get(position);
                BookDetailActivity.startDetailActivity(getActivity(), plateNovels.getNovelId());
            }
        });
        filterView.setiFilterViewCallbackListener(new IFilterViewCallbackListener() {
            @Override
            public void setITypeCallbackListener(int cate1, String cateLabel, int word1, String wordLabel, int serial1, String serialLabel) {
                cate = cate1;
                word = word1;
                serial = serial1;
                mCateLabel = cateLabel;
                mWordLabel = wordLabel;
                mSerialLabel = serialLabel;
                page = 0;
                mPresenter.quearBookList(cate, word, serial, channel, page);
            }
        });
        mSmartRefreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                mPresenter.quearBookList(cate, word, serial, channel, page);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 0;
                mPresenter.quearBookList(cate, word, serial, channel, page);
            }
        });
    }

    @Override
    protected void retryRefreshData() {
        mPresenter.quearFilter(channel);
        mPresenter.quearBookList(cate, word, serial, channel, page);
    }

    /**
     * 显示头部信息
     *
     * @param isshow
     */
    private void isHeader(boolean isshow) {
        if (isshow) {
            if (mCateLabel.equals("") && mWordLabel.equals("") && mSerialLabel.equals("")) {
                tvHeader.setText("已筛选： 全部");
            } else {
                tvHeader.setText("已筛选：" + mCateLabel + " " + mWordLabel + " " + mSerialLabel);
            }
            if (tvHeader.getVisibility() == View.GONE) {
                tvHeader.startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.fade_in));
                tvHeader.setVisibility(View.VISIBLE);

            }
        } else {
            if (tvHeader.getVisibility() == View.VISIBLE) {
                tvHeader.startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.push_up_out));
                tvHeader.setVisibility(View.GONE);
            }
        }
    }

    /***
     * 筛选和列表数据
     * @param list 数据集
     */
    @Override
    public void showBookList(List<PlateNovels> list) {
        if (page == 0) {
            monthlyMoreAdapter.setNewData(list);
        } else {
            monthlyMoreAdapter.addData(list);
        }
        isCliable = true;

    }

    /***
     *
     * @param bean
     */
    @Override
    public void showFilter(List<FilterBean> bean) {
        filterView.setFilterData(bean.get(0));
        filterView.setFilterData2(bean.get(1));
        filterView.setFilterData3(bean.get(2));

    }


    @Override
    public SmartRefreshLayout getSmartRefreshLayout() {
        return mSmartRefreshLayout;
    }

    @Override
    public void initData() {
        if (mPresenter != null) {
            mPresenter.quearFilter(channel);
            mPresenter.quearBookList(cate, word, serial, channel, page);
        }
    }

    @Override
    public void showContent() {
        dissLoading();
    }

    @Override
    public void showProgress() {
        isCliable = false;
        showLoading();
    }

    @Override
    public void showError(Throwable throwable) {
        ToastUtil.makeShortToast("网络异常");
        dissLoading();
        if (page == 0) {
            showNetErrorView(R.id.rlContent);
        }
    }

    @Override
    public void showEmpty() {

    }

    /**
     * 监听recyclerView滑动高度
     *
     * @param recyclerView
     * @return
     */
    public int getScollYDistance(RecyclerView recyclerView) {
        LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
        int position = layoutManager.findFirstVisibleItemPosition();
        View firstVisiableChildView = layoutManager.findViewByPosition(position);
        int itemHeight = firstVisiableChildView.getHeight();
        return (position) * itemHeight - firstVisiableChildView.getTop();
    }

}
